#include "SpriteExt.h"
#include "CCATFileCache.h"
#include "Box2D/Box2D.h"

SpriteExt::~SpriteExt()
{
}

SpriteExt* SpriteExt::spriteWithFile(const char *pszFileName)
{
	SpriteExt *pobSprite = new SpriteExt();
	if (pobSprite && pobSprite->initWithFile(pszFileName))
	{
		pobSprite->autorelease();
		return pobSprite;
	}
	CC_SAFE_DELETE(pobSprite);
	return NULL;
}

SpriteExt* SpriteExt::spriteWithFile(const char *pszFileName, const CCRect& rect)
{
	SpriteExt *pobSprite = new SpriteExt();
	if (pobSprite && pobSprite->initWithFile(pszFileName, rect))
	{
		pobSprite->autorelease();
		return pobSprite;
	}
	CC_SAFE_DELETE(pobSprite);
	return NULL;
}

void SpriteExt::dispear(const char* actName/* =NULL */, const char* actFile/* =NULL */)
{
	b2Body* pBody = (b2Body*)getUserData();
	if (pBody)
	{
		pBody->GetWorld()->DestroyBody(pBody);
		setUserData(NULL);
	}
	if (actFile && actName)
	{
		CCAction* pAction = CCATFileCache::sharedCCATFileCache()->findAction(actName, actFile);
		if (pAction)
		{
			runAction(pAction);
			return;
		}
	}
	
	CCAction* pAction = CCATFileCache::sharedCCATFileCache()->findAction(m_dispearAction.c_str(), m_ccatFile.c_str());
	if (pAction)
	{
		runAction(pAction);
	}
}

void SpriteExt::onExit()
{
	b2Body* pBody = (b2Body*)getUserData();
	if (pBody)
	{
		pBody->GetWorld()->DestroyBody(pBody);
		setUserData(NULL);
	}
    CCSprite::onExit();
}